package org.example;

import org.ehcache.config.builders.CacheConfigurationBuilder;
import org.ehcache.config.builders.ExpiryPolicyBuilder;
import org.ehcache.config.builders.ResourcePoolsBuilder;
import org.ehcache.config.units.EntryUnit;
import org.ehcache.jsr107.Eh107Configuration;

import javax.cache.Cache;
import javax.cache.CacheManager;
import javax.cache.Caching;
import javax.cache.configuration.Configuration;
import javax.cache.configuration.MutableConfiguration;
import javax.cache.expiry.CreatedExpiryPolicy;
import javax.cache.spi.CachingProvider;
import java.time.Duration;
import java.util.concurrent.TimeUnit;

/**
 * Hello world!
 *
 */
public class Ehcache3Jcache2
{
    public static void main( String[] args )
    {
        // 获取 CachingProvider
        CachingProvider provider =
                Caching.getCachingProvider();

        // 获取 CacheManager
        CacheManager cacheManager =
                provider.getCacheManager();

        // 2. 单独配置 Cache A：最多 1000 个条目
        CacheConfigurationBuilder<Object, Object> configE =
                CacheConfigurationBuilder.newCacheConfigurationBuilder(
                        Object.class, Object.class,
                        ResourcePoolsBuilder.newResourcePoolsBuilder().heap(1000, EntryUnit.ENTRIES) // ✅ 限制为 1000 个条目
                ).withExpiry(ExpiryPolicyBuilder.timeToIdleExpiration(Duration.ofMinutes(10)));
        // 配置缓存
        // 配置缓存
        Configuration<Object, Object> config = Eh107Configuration.fromEhcacheCacheConfiguration(configE);
        // 创建缓存
        Cache<Object, Object> cache = cacheManager.createCache("jcache-user", config);

        // 使用
        cache.put(1L, "Charlie");
        cache.put(2L, "Charlie11");
        cache.put(3L, "Charlie1122");
        cache.put(4L, "Charlie112233");


        // 缓存统计信息，在ehcache3下，已不再支持获取size，
        System.out.println( "Hello World!"+cache.get(1L) );
    }
}
